/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package vista;
import controlador.Utilidades;
import vista.modeloTablas.ModeloTablaLote;
import controlador.servicios.ServicioLoteProducto;
import controlador.servicios.ServicioProducto;
import java.util.Date;
import javax.swing.JOptionPane;
import vista.utilidades.EventoTecladoDecimal;
import vista.utilidades.EventoTecladoNumerico;
import vista.utilidades.FacadeComponentes;
/**
 *
 * @author Alex
 */
public class PanelAdministarLotes extends javax.swing.JPanel {
    private ModeloTablaLote modelo = new ModeloTablaLote();
    private ServicioLoteProducto sp = new ServicioLoteProducto();
    /**
     * Creates new form PanelAdministarProductos
     */
    public PanelAdministarLotes() {
        initComponents();
        cargarTabla();
        tabbed.setSelectedIndex(1);
        tabbed.setEnabledAt(0, false);
        cargarCombo();
    }
    private void cargarCombo(){
        cbx_producto.removeAllItems();
        FacadeComponentes.cargarCombos(cbx_producto, "producto");
        txt_precio.addKeyListener(new EventoTecladoDecimal(txt_precio));
        txt_utilidad.addKeyListener(new EventoTecladoDecimal(txt_utilidad));
        txt_stock.addKeyListener(new EventoTecladoNumerico(txt_stock));
    }
    private void cargarTabla() {
        modelo.setLista(sp.listar());
        tbl_tabla.setModel(modelo);
        tbl_tabla.updateUI();
    }

    private void irNuevo() {
        tabbed.setSelectedIndex(0);
        tabbed.setEnabledAt(0, true);
        tabbed.setEnabledAt(1, false);
        txt_codigo.setText("SB0000"+modelo.getLista().size()+1);
    }

    private void irListar() {
        tabbed.setSelectedIndex(1);
        tabbed.setEnabledAt(0, false);
        tabbed.setEnabledAt(1, true);
        tabbed.setTitleAt(0, "CREAR");
    }
    private void cargarObjeto() {
        sp.getLoteProducto().setCodBarra_lote(txt_codigo.getText());
        sp.getLoteProducto().setNro_compra(txt_nroventa.getText());
        sp.getLoteProducto().setPc_lote(Double.parseDouble(txt_precio.getText()));
        sp.getLoteProducto().setPv_lote(Double.parseDouble(txt_precioVenta.getText()));        
        sp.getLoteProducto().setStock(Integer.parseInt(txt_stock.getText()));
        sp.getLoteProducto().setProducto(FacadeComponentes.obtenerProductoCombo(cbx_producto));
    }

    private void limpiaCampos() {
        txt_codigo.setText("");
        txt_nroventa.setText("");
        txt_precio.setText("");
        txt_precioVenta.setText("");
        txt_stock.setText("");        
        txt_utilidad.setText("");
        cbx_producto.setSelectedIndex(0);
        sp.nuevaInstancia();
    }

    private void cargarVista() {
        txt_codigo.setText(sp.getLoteProducto().getCodBarra_lote());
        txt_nroventa.setText(sp.getLoteProducto().getNro_compra());
        txt_precio.setText(sp.getLoteProducto().getPc_lote()+"");
        txt_precioVenta.setText(sp.getLoteProducto().getPv_lote()+"");
        txt_stock.setText(sp.getLoteProducto().getStock()+"");
        double utilidad = ((sp.getLoteProducto().getPv_lote()-sp.getLoteProducto().getPc_lote())/sp.getLoteProducto().getPc_lote())*100;
        txt_utilidad.setText(utilidad+"");
        cbx_producto.setSelectedItem(sp.getLoteProducto().getProducto());
    }
    private void cargarDatosProducto(){
        txt_producto.setText("");
        ServicioProducto aux = new ServicioProducto();
        aux.fijarInstancia(FacadeComponentes.obtenerProductoCombo(cbx_producto));
        txt_producto.setText("Tipo: "+aux.getProducto().getTipo()+"\n"+"Marca: "+aux.getProducto().getMarca()+"\n"+
                "Modelo: "+aux.getProducto().getModelo()+"\n"+"Categoría: "+aux.getProducto().getCategoria()+"\n"+"Descripción: "+aux.getProducto().getDescripcion()+"\n");
    }
    private void calcularPv(){
        double precioc = 0;
        double utilidad=0;
        double suma=0;
        double pvp=0;
        try {
            precioc = Double.parseDouble(txt_precio.getText());
            utilidad = Double.parseDouble(txt_utilidad.getText());
        } catch (Exception e) {
        }
        suma = (precioc*utilidad)/100;
        pvp = precioc+suma;
        txt_precioVenta.setText(String.valueOf(pvp));
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        tabbed = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        txt_codigo = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        txt_precio = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        txt_utilidad = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        txt_precioVenta = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        txt_stock = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        txt_nroventa = new javax.swing.JTextField();
        jLabel12 = new javax.swing.JLabel();
        cbx_producto = new javax.swing.JComboBox();
        jScrollPane2 = new javax.swing.JScrollPane();
        txt_producto = new javax.swing.JTextArea();
        btnGuardar = new javax.swing.JButton();
        btnCancelarA = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jPanel4 = new javax.swing.JPanel();
        jLabel9 = new javax.swing.JLabel();
        cmbCriterioProducto = new javax.swing.JComboBox();
        jLabel10 = new javax.swing.JLabel();
        txtBusquedaProducto = new javax.swing.JTextField();
        jPanel5 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbl_tabla = new javax.swing.JTable();
        btnNuevoProducto = new javax.swing.JButton();
        btnModificarProducto = new javax.swing.JButton();
        btnEliminarProducto = new javax.swing.JButton();
        btnCancelarB = new javax.swing.JButton();

        setBorder(javax.swing.BorderFactory.createTitledBorder("ADMINISTRAR LOTES"));

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Datos Informativos Producto"));

        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel3.setText("Código Barras:");

        txt_codigo.setToolTipText("Este código es autogenerado de acuerdo a la categoría");

        jLabel5.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel5.setText("Precio Compra: ");

        txt_precio.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txt_precioKeyReleased(evt);
            }
        });

        jLabel6.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel6.setText("Utilidad:");

        txt_utilidad.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txt_utilidadKeyReleased(evt);
            }
        });

        jLabel7.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel7.setText("Precio Venta:");

        txt_precioVenta.setEnabled(false);

        jLabel8.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel8.setText("Stock:");

        jLabel11.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel11.setText("Nro Venta:");

        txt_nroventa.setToolTipText("Este código es autogenerado de acuerdo a la categoría");

        jLabel12.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel12.setText("Producto:");

        cbx_producto.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cbx_producto.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cbx_productoItemStateChanged(evt);
            }
        });

        txt_producto.setColumns(20);
        txt_producto.setRows(5);
        jScrollPane2.setViewportView(txt_producto);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel12, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(cbx_producto, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(txt_codigo))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(txt_stock))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(txt_precio, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel3Layout.createSequentialGroup()
                                .addGap(23, 23, 23)
                                .addComponent(jLabel7)
                                .addGap(23, 23, 23))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                                .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(41, 41, 41)))
                        .addGap(11, 11, 11)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(txt_precioVenta, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 191, Short.MAX_VALUE)
                            .addComponent(txt_utilidad)))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel11, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(txt_nroventa, javax.swing.GroupLayout.PREFERRED_SIZE, 191, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGap(23, 23, 23)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 327, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(24, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel11)
                        .addComponent(txt_nroventa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel3)
                        .addComponent(txt_codigo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel6, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel5)
                        .addComponent(txt_precio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(txt_utilidad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(txt_precioVenta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8)
                    .addComponent(txt_stock, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel12)
                            .addComponent(cbx_producto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 10, Short.MAX_VALUE)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())))
        );

        btnGuardar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/vista/images/save.png"))); // NOI18N
        btnGuardar.setText("Guardar");
        btnGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGuardarActionPerformed(evt);
            }
        });

        btnCancelarA.setIcon(new javax.swing.ImageIcon(getClass().getResource("/vista/images/reload.png"))); // NOI18N
        btnCancelarA.setText("Cancelar");
        btnCancelarA.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarAActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(btnGuardar, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btnCancelarA, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCancelarA, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnGuardar, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(48, 48, 48))
        );

        tabbed.addTab("NUEVO", jPanel1);

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder("Criterio de Busqueda"));

        jLabel9.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel9.setText("Seleccione el Criterio:");

        cmbCriterioProducto.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Todos", "Por Código Barras", "Por Tipo", "Por Categoría", "Por Marca" }));
        cmbCriterioProducto.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cmbCriterioProductoItemStateChanged(evt);
            }
        });

        jLabel10.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel10.setText("Ingrese Búsqueda: ");

        txtBusquedaProducto.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtBusquedaProductoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, 166, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cmbCriterioProducto, javax.swing.GroupLayout.PREFERRED_SIZE, 148, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jLabel10, javax.swing.GroupLayout.PREFERRED_SIZE, 153, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtBusquedaProducto)
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(cmbCriterioProducto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel10)
                    .addComponent(txtBusquedaProducto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel5.setBorder(javax.swing.BorderFactory.createTitledBorder("Resultados de Búsqueda"));
        jPanel5.setLayout(new java.awt.BorderLayout());

        tbl_tabla.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(tbl_tabla);

        jPanel5.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        btnNuevoProducto.setIcon(new javax.swing.ImageIcon(getClass().getResource("/vista/images/add-item.png"))); // NOI18N
        btnNuevoProducto.setText("Nuevo");
        btnNuevoProducto.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNuevoProductoActionPerformed(evt);
            }
        });

        btnModificarProducto.setIcon(new javax.swing.ImageIcon(getClass().getResource("/vista/images/edit.png"))); // NOI18N
        btnModificarProducto.setText("Modificar");
        btnModificarProducto.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnModificarProductoActionPerformed(evt);
            }
        });

        btnEliminarProducto.setIcon(new javax.swing.ImageIcon(getClass().getResource("/vista/images/delete-item.png"))); // NOI18N
        btnEliminarProducto.setText("Eliminar");
        btnEliminarProducto.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEliminarProductoActionPerformed(evt);
            }
        });

        btnCancelarB.setIcon(new javax.swing.ImageIcon(getClass().getResource("/vista/images/reload.png"))); // NOI18N
        btnCancelarB.setText("Cancelar");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addGap(0, 125, Short.MAX_VALUE)
                        .addComponent(btnNuevoProducto, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btnModificarProducto, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btnEliminarProducto, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btnCancelarB, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, 182, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCancelarB, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnEliminarProducto, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnModificarProducto, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnNuevoProducto, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(22, Short.MAX_VALUE))
        );

        tabbed.addTab("LISTAR LOTES", jPanel2);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(tabbed)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(tabbed, javax.swing.GroupLayout.PREFERRED_SIZE, 404, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(37, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnNuevoProductoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNuevoProductoActionPerformed
        // TODO add your handling code here:
        irNuevo();
    }//GEN-LAST:event_btnNuevoProductoActionPerformed

    private void btnCancelarAActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarAActionPerformed
        // TODO add your handling code here:
        irListar();
        limpiaCampos();
    }//GEN-LAST:event_btnCancelarAActionPerformed

    private void btnGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGuardarActionPerformed
        // TODO add your handling code here:
        if(Utilidades.requerido(txt_codigo, txt_codigo.getText())==true&&
            Utilidades.requerido(txt_precio, txt_precio.getText())==true&&
                Utilidades.requerido(txt_precioVenta, txt_precioVenta.getText())==true&&
                Utilidades.requerido(txt_stock, txt_stock.getText())==true){
            cargarObjeto();
            if(sp.getLoteProducto().getId_lote()==null){
                if(sp.guardar()==true){
                    JOptionPane.showMessageDialog(this, "Se ha guardado", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                    cargarTabla();
                    irListar();
                    limpiaCampos();
                }else{
                    JOptionPane.showMessageDialog(this, "No se ha guardado", "Error", JOptionPane.ERROR_MESSAGE);
                }
            }else{
                if(sp.guardar()==true){
                    JOptionPane.showMessageDialog(this, "Se ha modificado", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                    cargarTabla();
                    irListar();
                    limpiaCampos();
                }else{
                    JOptionPane.showMessageDialog(this, "No se ha modificado", "Error", JOptionPane.ERROR_MESSAGE);
                }
            }
        }
    }//GEN-LAST:event_btnGuardarActionPerformed

    private void btnModificarProductoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnModificarProductoActionPerformed
        // TODO add your handling code here:
        int pos = tbl_tabla.getSelectedRow();
        if (pos >= 0) {
            sp.fijarInstancia(modelo.getLista().get(pos));
            irNuevo();
            cargarVista();
            
            tbl_tabla.clearSelection();
            tabbed.setTitleAt(0, "MODIDICAR");
        } else {
            JOptionPane.showMessageDialog(this, "Seleccione un lote", "Error", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_btnModificarProductoActionPerformed

    private void btnEliminarProductoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEliminarProductoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_btnEliminarProductoActionPerformed

    private void cbx_productoItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cbx_productoItemStateChanged
        // TODO add your handling code here:
        cargarDatosProducto();
    }//GEN-LAST:event_cbx_productoItemStateChanged

    private void txt_precioKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txt_precioKeyReleased
        // TODO add your handling code here:
        calcularPv();
    }//GEN-LAST:event_txt_precioKeyReleased

    private void txt_utilidadKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txt_utilidadKeyReleased
        // TODO add your handling code here:
        calcularPv();
    }//GEN-LAST:event_txt_utilidadKeyReleased

    private void txtBusquedaProductoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtBusquedaProductoActionPerformed
        // TODO add your handling code here:
        buscar();
    }//GEN-LAST:event_txtBusquedaProductoActionPerformed

    private void cmbCriterioProductoItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cmbCriterioProductoItemStateChanged
        // TODO add your handling code here:
        buscar();
    }//GEN-LAST:event_cmbCriterioProductoItemStateChanged
    private void buscar(){        
        String criterio = "";
        String texto = txtBusquedaProducto.getText();
        if(cmbCriterioProducto.getSelectedIndex()==0){
            cargarTabla();
        }else{
            if(cmbCriterioProducto.getSelectedIndex()==3){
                criterio=" p.producto.categoria.nombre_categoria";
            }
            if(cmbCriterioProducto.getSelectedIndex()==2){
                criterio=" p.producto.tipo.nombre_tipo";
            }
            if(cmbCriterioProducto.getSelectedIndex()==4){
                criterio=" p.producto.marca.nombre_marca";
            }
            if(cmbCriterioProducto.getSelectedIndex()==1){
                criterio=" p.codBarra_lote";
            }
            modelo.setLista(sp.buscar(criterio, texto));
            tbl_tabla.updateUI();
        }
        
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelarA;
    private javax.swing.JButton btnCancelarB;
    private javax.swing.JButton btnEliminarProducto;
    private javax.swing.JButton btnGuardar;
    private javax.swing.JButton btnModificarProducto;
    private javax.swing.JButton btnNuevoProducto;
    private javax.swing.JComboBox cbx_producto;
    private javax.swing.JComboBox cmbCriterioProducto;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTabbedPane tabbed;
    private javax.swing.JTable tbl_tabla;
    private javax.swing.JTextField txtBusquedaProducto;
    private javax.swing.JTextField txt_codigo;
    private javax.swing.JTextField txt_nroventa;
    private javax.swing.JTextField txt_precio;
    private javax.swing.JTextField txt_precioVenta;
    private javax.swing.JTextArea txt_producto;
    private javax.swing.JTextField txt_stock;
    private javax.swing.JTextField txt_utilidad;
    // End of variables declaration//GEN-END:variables
}
